package kz.kstu.scantest.business.server.impl;

import kz.kstu.scantest.business.entity.Identifiable;
import kz.kstu.scantest.business.server.context.AbstractServerContextManager;

import javax.persistence.EntityManager;

/**
 * @author Filipp_Stankevich
 *         created: 26.07.11 19:41
 */
public abstract class AbstractService extends AbstractServerContextManager {

    protected EntityManager getEntityManager() {
        return getServerContext().getEntityManager();
    }

    public <T extends Identifiable> T save(T object) {
        if (object.getId() != null) {
            return getEntityManager().merge(object);
        } else {
            getEntityManager().persist(object);
            return object;
        }
    }

    public void remove(Class objectClass, Long id) {
        getEntityManager().remove(getEntityManager().find(objectClass, id));
    }
}
